//
//  ZZPrivacyController.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/8.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZPrivacyController.h"

#import "SettingCell.h"

#define cellIdentifier @"SettingCell"

@interface ZZPrivacyController (){
    int allow_message;
    int allow_guestbook;
}

@property(nonatomic,strong)UITableView      *listTable;
@property(nonatomic,strong)NSArray   *titleArr;

@end

@implementation ZZPrivacyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"隐私管理";
    [self setNavigationBar];
    
    self.navigationController.navigationBarHidden = NO;
    
    // Do any additional setup after loading the view.
    [self createTableView];
    [_listTable reloadData];
    
}

-(void)createTableView{
    _titleArr = @[@[
                      @{@"title":@"谁可以发私信给我",@"value":@"所有人",@"code":@"1"},
                      @{@"title":@"谁可以给我留言",@"value":@"所有人",@"code":@"2"}]];
    
    _listTable=[self.view createTableView:self cell:cellIdentifier];
    [_listTable setFrame:self.view.bounds];
    [_listTable setBackgroundColor:UIColor.whiteColor];
    if (iOS7) {
        _listTable.backgroundView = nil;
    }
    [_listTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self getPrivacyinfo];
}

-(void)setReSetArray{
    NSArray *t = @[@"拒绝所有人",@"所有人",@"我关注的人"];
    
    _titleArr = @[@[
                      @{@"title":@"谁可以发私信给我",@"value":t[allow_message],@"code":@"1"},
                      @{@"title":@"谁可以给我留言",@"value":t[allow_guestbook],@"code":@"2"}]];
    
    [_listTable reloadData];
}


-(void)getPrivacyinfo{
    [NetworkUtil POST:API_privacyinfo parameters:@{@"session_uid":[UserUtil getUser].user_id} success:^(id responseObject, NSDictionary *dict) {
        allow_message = [dict[@"data"][@"allow_message"] intValue];
        allow_guestbook = [dict[@"data"][@"allow_guestbook"] intValue];
        if(allow_message > 2 || allow_message < 0){
            allow_message  = 0;
        }
        
        if(allow_guestbook < 0 || allow_guestbook > 2){
            allow_guestbook = 0;
        }
        [self setReSetArray];
        
    } failure:^(NSError *error, NSString *message) {
        
    }];
}


-(IBAction)commitButtonClick:(id)sender{
    [[AlertUtil shareInstance] showAlert:@"确定要退出登录吗?" message:@"" cancelTitle:@"取消" titleArray:@[@"确定"] viewController:self confirm:^(NSInteger buttonTag) {
        if(buttonTag==0){
            
            
        }
    }];
}


#pragma mark UITableView delegate Start
// 返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titleArr.count;
}

// 返回section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0f;
}

// 返回section 的View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

// 返回section下得行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_titleArr.count == 0){
        return 0;
    }
    NSArray *items = _titleArr[section];
    return items.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingCell *cell = (SettingCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSArray *items = _titleArr[indexPath.section];
    [cell dataToView:items[indexPath.row]];
    cell.labLine.hidden = YES;
    
    return cell;
}

// table 行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

// table 行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    VipModel *model=[_listArray objectAtIndex:indexPath.section];
    NSDictionary *item = _titleArr[indexPath.section][indexPath.row];
    int code = [item[@"code"] intValue];
    
    [[AlertUtil shareInstance] showSheet:nil message:nil cancelTitle:@"取消" titleArray:@[@"拒绝所有人",@"所有人",@"我关注的人"] viewController:self confirm:^(NSInteger buttonTag) {
        if(code == 1){
            // 私信
            
            allow_message = buttonTag;
        }
        else{
            // 留言
            allow_guestbook = buttonTag;
        }
        NSDictionary *p = @{@"session_uid":[UserUtil getUser].user_id,
                            @"allow_message":convertIntToString(allow_message),
                            @"allow_guestbook":convertIntToString(allow_guestbook)
                            };
        
        [NetworkUtil POST:API_setprivacy parameters:p success:^(id responseObject, NSDictionary *dict) {
            [self setReSetArray];
        } failure:^(NSError *error, NSString *message) {
            [PopUtil showMessage:message];
        }];
        // buttonTag 0所有人，1关注的人
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
